package com.smartcommunity.service.waterEleGasUsage.impl;

import com.smartcommunity.dao.waterEleGasUsage.VillageWaterEleGasUsageMapper;
import com.smartcommunity.entity.waterEleGasUsage.VillageWaterEleGasUsage;
import com.smartcommunity.service.waterEleGasUsage.VillageWaterEleGasUsageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author nieming
 * @descrition
 * @date 2020/5/15
 */
@Service
public class VillageWaterEleGasUsageServiceImpl implements VillageWaterEleGasUsageService {

    @Autowired
    private VillageWaterEleGasUsageMapper villageWaterEleGasUsageMapper;

    @Override
    public void saveOrUpdate(List<VillageWaterEleGasUsage> villageWaterEleGasUsages) {
        villageWaterEleGasUsages.parallelStream().forEach(villageWaterEleGasUsage -> {
            if (isExits(villageWaterEleGasUsage.getVillageCode(), villageWaterEleGasUsage.getUsageDate())) {
                villageWaterEleGasUsageMapper.updateByVillageAndDate(villageWaterEleGasUsage);
            } else {
                villageWaterEleGasUsage.setMrowTime(new Date());
                villageWaterEleGasUsageMapper.insert(villageWaterEleGasUsage);
            }
        });
    }

    public boolean isExits(String villageCode, String usageDate) {
        int count = villageWaterEleGasUsageMapper.queryCountByVillageAndDate(villageCode, usageDate);
        return count > 0;
    }
}
